/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.SanPhamDAO;
import java.util.List;
import org.hibernate.Query;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pojo.SanPham;

/**
 *
 * @author MongKieu
 */
public class SanPhamDAOImpl extends HibernateDaoSupport implements SanPhamDAO {

    @Override
    public int soLuongSanPhamTheoTheLoai(int maTheLoai) {
        String myQuery = "select count(*) from SanPham where MaTheLoai=" + maTheLoai;
        int count = ((Long) getSession().createQuery(myQuery).uniqueResult()).intValue();
        return count;
    }

    @Override
    public List<SanPham> listSanPhamTheoTheLoai(int maTheLoai) {
        return getHibernateTemplate().find("from SanPham where MaTheLoai=" + maTheLoai);

    }

    @Override
    public List<SanPham> listSanPhamTheoTheLoaiCoPhanTrang(int maTheLoai, int viTriBatDau, int soLuongSanPham) {
        String myQuery = "from SanPham where theLoai.id=" + maTheLoai;
        return getSession().createQuery(myQuery).setFirstResult(viTriBatDau).setMaxResults(soLuongSanPham).list();
    }

    @Override
    public boolean kiemtraSanPhamTonTai(int maSanPham) {
        boolean result = false;
        String myquery = "from SanPham where Id=" + maSanPham;
        Query q = getSession().createQuery(myquery);
        List list = q.list();
        int length = list.size();
        if (length > 0) {
            result = true;
        }
        return result;
    }

    @Override
    public SanPham laySanPhamByMaSanPham(int maSanPham) {
        String myquery = "from SanPham where Id=" + maSanPham;
        Query q = getSession().createQuery(myquery);
        List list = q.list();
        return (SanPham) list.get(0);
    }

    @Override
    public boolean kiemtraSanPhamTonTaiByISBN(String ISBN) {
        boolean result = false;
          String myquery = "from SanPham where  ISBN like '"+ISBN+"'";
        //String myquery = "from SanPham where ISBN=" + ISBN;
        Query q = getSession().createQuery(myquery);
        List list = q.list();
        int length = list.size();
        if (length > 0) {
            result = true;
        }
        return result;
    }

    @Override
    public SanPham laySanPhamByISBN(String ISBN) {
        //String myquery = "from SanPham where ISBN=" + ISBN;
        String myquery = "from SanPham where  ISBN like '"+ISBN+"'";
        Query q = getSession().createQuery(myquery);
        List list = q.list();
        return (SanPham) list.get(0);
    }

    @Override
    public List<SanPham> listSanPhamMoiNhapVe(int soLuongSanPhamMax, String myQuery) {
        //String myQuery = "from SanPham order by NgayNhap- current_timestamp()  desc ";
        return getSession().createQuery(myQuery).setFirstResult(0).setMaxResults(soLuongSanPhamMax).list();
    }

    @Override
    public List<SanPham> listSanPhamMoiNhapVeCoPhanTrang(int viTriBatDau, int soLuongSanPham, String myQuery) {
        
        return getSession().createQuery(myQuery).setFirstResult(viTriBatDau).setMaxResults(soLuongSanPham).list();
    }

    @Override
    public int soLuongAllSanPhamTrongNhaSach() {
        String myQuery = "select count(*) from SanPham";
        int count = ((Long) getSession().createQuery(myQuery).uniqueResult()).intValue();
        return count;
    }

    @Override
    public List<SanPham> listAllSanPhamTrongNhaSachCoPhanTrang(int vitriBatDau, int soLuongSanPham) {
        String myQuery = "from SanPham";
        return getSession().createQuery(myQuery).setFirstResult(vitriBatDau).setMaxResults(soLuongSanPham).list();
    }

    @Override
    public List<SanPham> listSanPhamTheoTenVaNhomSanPhamCoPhanTrang(String tenSanPham, int NhomSanPhamID, int viTriBatDau, int soLuongSanPham, String myQuery) {
        return getSession().createQuery(myQuery).setFirstResult(viTriBatDau).setMaxResults(soLuongSanPham).list();

    }

    @Override
    public List<SanPham> listSanPhamTheoTenVaNhomSanPham(String tenSanPham, int NhomSanPhamID, String myQuery) {
        return getHibernateTemplate().find(myQuery);

    }
}
